package com.zoryang.commonutils

import android.annotation.SuppressLint
import java.text.SimpleDateFormat
import java.util.*
import kotlin.math.roundToLong

/**
 * @author Morphling
 */
object TimeUtil {

    const val FORMAT_yyyy_MM_dd = "yyyy-MM-dd"
    const val FORMAT_yyyy_MM_dd_A = "yyyy-MM-dd a"
    const val FORMAT_YYYYMM = "yyyyMM"
    const val FORMAT_YYYY_MM = "yyyy年MM月"
    const val FORMAT_MM_dd = "MM月dd日"
    const val FORMAT_YYYY_MM_dd = "yyyy年MM月dd日"
    const val FORMAT_yyyyMMdd = "yyyyMMdd"
    const val FORMAT_yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss"
    const val FORMAT_yyyy_MM_dd_HH = "yyyy-MM-dd HH"
    const val FORMAT_24 = "HH:mm:ss"
    const val FORMAT_YEAR = "yyyy"
    const val FORMAT_MM = "mm"
    const val FORMAT_HH_MM = "HH:mm"
    const val FORMAT_YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm"
    const val FORMAT_YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss:SSS"
    const val FORMAT_YYYY_MM_DD_HH_MM_CH = "yyyy年MM月dd日 HH:mm"
    const val FORMAT_MM_DD_HH_MM_CH = "MM月dd日 HH:mm"
    const val FORMAT_MM_DD_HH_MM = "MM-dd HH:mm"
    const val FORMAT_DD_HH_MM_CH = "dd日 HH:mm"
    const val FORMAT_HH_MM_SS = "HH小时mm分ss秒"


    fun getCurrentTime() = System.currentTimeMillis()

    /**
     * 获取当前年
     */
    fun getCurrentYear(): Int = Calendar.getInstance().get(Calendar.YEAR)

    /**
     * 获取当前月
     */
    fun getCurrentMonth(): Int = Calendar.getInstance().get(Calendar.MONTH) + 1

    /**
     * 获取当前是几点
     */
    fun getCurrentHour(): Int = Calendar.getInstance().get(Calendar.HOUR_OF_DAY)

    /**
     * 获取当前是几号
     */
    fun getCurrentDay(): Int = Calendar.getInstance().get(Calendar.DAY_OF_MONTH)


    /**
     *当前年毫秒
     */
    fun getTimeOfYearStart(): Long {
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.HOUR_OF_DAY, 0)
        calendar.clear(Calendar.MINUTE)
        calendar.clear(Calendar.SECOND)
        calendar.clear(Calendar.MILLISECOND)
        calendar.set(Calendar.DAY_OF_YEAR, 1)
        return calendar.timeInMillis
    }

    /**
     *当前月毫秒
     */
    fun getTimeOfMonthStart(): Long {
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.HOUR_OF_DAY, 0)
        calendar.clear(Calendar.MINUTE)
        calendar.clear(Calendar.SECOND)
        calendar.clear(Calendar.MILLISECOND)
        calendar.set(Calendar.DAY_OF_MONTH, 1)
        return calendar.timeInMillis
    }

    /**
     *当前周毫秒
     */
    fun getTimeOfWeekStart(): Long {
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.HOUR_OF_DAY, 0)
        calendar.clear(Calendar.MINUTE)
        calendar.clear(Calendar.SECOND)
        calendar.clear(Calendar.MILLISECOND)
        calendar.set(Calendar.DAY_OF_WEEK, calendar.firstDayOfWeek)
        return calendar.timeInMillis
    }

    /**
     * 日期大小比较
     * @param endtime 计划结束日期
     * @param time    当前日期
     * @return endtime>time true ,endtime<time true
     */
    @SuppressLint("SimpleDateFormat")
    fun dateCompare(endtime: Long, time: Long, pattern: String = FORMAT_yyyy_MM_dd): Boolean {
        var result = false
        val sdf = SimpleDateFormat(pattern)
        kotlin.runCatching {
            val dt1 = sdf.parse(sdf.format(Date(endtime)))
            val dt2 = sdf.parse(sdf.format(Date(time)))
            if (dt1.time >= dt2.time) {
                result = true
            }
        }
        return result
    }

    /**
     * 判断是否为当前月
     * @return true 是当前月，false 不是当前月
     */
    @SuppressLint("SimpleDateFormat")
    fun isCurrentMonth(time: Long): Boolean =
        getCurrentMonth() - SimpleDateFormat(FORMAT_MM).format(Date(time)).toInt() == 0

//    /**
//     * 此方法用于获取  HH:mm 格式的时间
//     * @return 返回的是  HH:mm  类型的时间
//     */
//    fun hhmm(): Int {
//        val formatter = SimpleDateFormat(FORMAT_MM, Locale.getDefault())
//        val m = formatter.format(Date(getCurrentTime()))
//        return if (m.startsWith("0")) {
//            Integer.decode(m.substring(1, 2))
//        } else {
//            Integer.decode(m)
//        }
//    }
    /**
     * 字符串格式化
     * @return pattern格式的字符串
     */
    @SuppressLint("SimpleDateFormat")
    fun stringFormat(date: Long, pattern: String = FORMAT_yyyy_MM_dd): String =
        SimpleDateFormat(pattern).format(Date(date))

    /**
     * 字符串格式化
     * @return pattern格式的字符串
     */
    @SuppressLint("SimpleDateFormat")
    fun stringFormat(date: String, pattern: String = FORMAT_yyyy_MM_dd): String =
        SimpleDateFormat(pattern).format(Date(date.toLong()))


    /**
     * yyyy-MM-dd 转 Long
     * @param dateString "2020-10-31"
     * @return 时间毫秒值：1642489226892
     */
    @SuppressLint("SimpleDateFormat")
    fun dateStringParse(dateString: String, pattern: String = FORMAT_yyyy_MM_dd): Long {
        kotlin.runCatching {
            return SimpleDateFormat(pattern).parse(dateString).time
        }
        return 0
    }

    /**
     * 字符串转日期
     */
    fun string2Date(dateString: String, pattern: String = FORMAT_yyyy_MM_dd): Date? {
        var date: Date? = null
        kotlin.runCatching {
            date = SimpleDateFormat(pattern, Locale.getDefault()).parse(dateString)
        }
        return date
    }

    /**
     * 秒转 00:00:00
     *
     * @param time
     * @return
     */
    fun mills2Hms(time: Long): String? {
        val shi = time / 3600
        val fen = time % 3600 / 60
        val miao = time % 3600 % 60
        //shi< 10 ? ("0" + shi) : shi)判断时否大于10时的话就执行shi,否则执行括号中的
        return (if (shi < 10) "0$shi" else shi).toString() + ":" + (if (fen < 10) "0$fen" else fen) + ":" + if (miao < 10) "0$miao" else miao
    }

    /**
     * 毫秒转 00:00
     */
    fun mills2Hm(mills: Long): String? {
        val minute = mills / 60000
        val second = (mills % 60000 / 1000f).roundToLong()
        return getStr(minute) + ":" + getStr(second)
    }

    /**
     * 是否是今天
     * @return true:是今天 , false:不是今天
     */
    @SuppressLint("SimpleDateFormat")
    fun isToday(date: Date): Boolean {
        val dateFormat = SimpleDateFormat(FORMAT_yyyy_MM_dd)
        return dateFormat.format(date) == dateFormat.format(Date())
    }

    private fun getStr(num: Long): String = if (num < 10) "0$num" else "$num"
}